/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.FaceBase;
import com.yami.shop.bean.enums.FaceTypeEnum;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.FaceBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 人脸底图管理控制器
 * @author
 */
@RestController
@RequestMapping("/admin/faceBase")
public class FaceBaseController {

    @Autowired
    private FaceBaseService faceBaseService;

    /**
     * 分页获取人脸底图列表
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:page')")
    public ServerResponseEntity<IPage<FaceBase>> page(FaceBase faceBase, PageParam<FaceBase> page) {
        IPage<FaceBase> faceBasePage = faceBaseService.page(page, new LambdaQueryWrapper<FaceBase>()
                .like(StrUtil.isNotBlank(faceBase.getFaceName()), FaceBase::getFaceName, faceBase.getFaceName())
                .eq(faceBase.getFaceType() != null, FaceBase::getFaceType, faceBase.getFaceType())
                .eq(faceBase.getStatus() != null, FaceBase::getStatus, faceBase.getStatus()));
        return ServerResponseEntity.success(faceBasePage);
    }

    /**
     * 获取人脸底图详情
     */
    @GetMapping("/info/{faceBaseId}")
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:info')")
    public ServerResponseEntity<FaceBase> info(@PathVariable("faceBaseId") String faceBaseId) {
        FaceBase faceBase = faceBaseService.getById(faceBaseId);
        return ServerResponseEntity.success(faceBase);
    }

    /**
     * 新增人脸底图
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:save')")
    public ServerResponseEntity<Void> save(@RequestBody FaceBase faceBase) {
        faceBase.setCreateTime(new Date());
        faceBase.setUpdateTime(new Date());
        faceBaseService.save(faceBase);
        return ServerResponseEntity.success();
    }

    /**
     * 修改人脸底图
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:update')")
    public ServerResponseEntity<Void> update(@RequestBody FaceBase faceBase) {
        faceBase.setUpdateTime(new Date());
        faceBaseService.updateById(faceBase);
        return ServerResponseEntity.success();
    }

    /**
     * 删除人脸底图
     */
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody String[] faceBaseIds) {
        faceBaseService.removeByIds(Arrays.asList(faceBaseIds));
        return ServerResponseEntity.success();
    }

    /**
     * 更新人脸底图状态
     */
    @PutMapping("/status")
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:update')")
    public ServerResponseEntity<Void> updateStatus(@RequestBody FaceBase faceBase) {
        FaceBase updateFaceBase = new FaceBase();
        updateFaceBase.setFaceBaseId(faceBase.getFaceBaseId());
        updateFaceBase.setStatus(faceBase.getStatus());
        updateFaceBase.setUpdateTime(new Date());
        faceBaseService.updateById(updateFaceBase);
        return ServerResponseEntity.success();
    }

    /**
     * 批量更新人脸底图状态
     */
    @PutMapping("/batchStatus")
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:update')")
    public ServerResponseEntity<Void> batchUpdateStatus(@RequestBody BatchStatusRequest request) {
        if (request.getFaceBaseIds() == null || request.getFaceBaseIds().isEmpty()) {
            return ServerResponseEntity.showFailMsg("人脸底图ID列表不能为空");
        }
        if (request.getStatus() == null) {
            return ServerResponseEntity.showFailMsg("状态不能为空");
        }

        for (String faceBaseId : request.getFaceBaseIds()) {
            FaceBase updateFaceBase = new FaceBase();
            updateFaceBase.setFaceBaseId(Long.valueOf(faceBaseId));
            updateFaceBase.setStatus(request.getStatus());
            updateFaceBase.setUpdateTime(new Date());
            faceBaseService.updateById(updateFaceBase);
        }

        return ServerResponseEntity.success();
    }

    /**
     * 批量删除人脸底图
     */
    @DeleteMapping("/batch")
//    @PreAuthorize("@pms.hasPermission('admin:faceBase:delete')")
    public ServerResponseEntity<Void> batchDelete(@RequestBody BatchDeleteRequest request) {
        if (request.getFaceBaseIds() == null || request.getFaceBaseIds().isEmpty()) {
            return ServerResponseEntity.showFailMsg("人脸底图ID列表不能为空");
        }
        
        faceBaseService.removeByIds(request.getFaceBaseIds());
        return ServerResponseEntity.success();
    }
}

/**
 * 批量状态更新请求类
 */
class BatchStatusRequest {
    private List<String> faceBaseIds;
    private Integer status;

    public List<String> getFaceBaseIds() {
        return faceBaseIds;
    }

    public void setFaceBaseIds(List<String> faceBaseIds) {
        this.faceBaseIds = faceBaseIds;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

/**
 * 批量删除请求类
 */
class BatchDeleteRequest {
    private List<String> faceBaseIds;

    public List<String> getFaceBaseIds() {
        return faceBaseIds;
    }

    public void setFaceBaseIds(List<String> faceBaseIds) {
        this.faceBaseIds = faceBaseIds;
    }
}
